//
//  WBTitleButton.swift
//  XLWB
//
//  Created by MacBook on 16/7/4.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    init(title: String?) {
        
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: [])
            
        } else {
            setTitle(title! + " ", for: [])
            
            setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray(), for: [])
        
        sizeToFit()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(code:) has not been implemented")
    }
    
    //重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断label和imageView是否同时存在
        guard let titleLabel = titleLabel, imageView = imageView else {
            return
        }
        print("\(titleLabel) \(imageView)")
        
        titleLabel.frame.origin.x = 0
        imageView.frame.origin.x = titleLabel.bounds.width
    
    }
}
